package com.jyserver.common.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class JyHttp {
    private static Logger logger = Logger.getLogger(JyHttp.class);

    public static ResponseEntity<String> postweb(String url) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity entity = new HttpEntity("", headers);
        return restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
    }

    public static ResponseEntity<String> postweb(String url, String bodyValTemplate) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity entity = new HttpEntity(bodyValTemplate, headers);
        return restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
    }

    public static ResponseEntity<String> getweb(String url) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity entity = new HttpEntity("", headers);
        return restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
    }

    public static String openStreamUrl(String url) {
        URL ur = null;
//        System.err.println(url);
        StringBuffer sb = new StringBuffer();
        try {
            ur = new URL(url);
            BufferedReader reader = new BufferedReader(new InputStreamReader(ur.openStream(), "UTF-8"));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * @Description :获得前端返回值
     */
    public static <T> JSONObject getRequestString(HttpServletRequest request) {
        // 参数Map
        Map properties = request.getParameterMap();
        // 返回值Map
        Map returnMap = new HashMap();
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        Object value = "";
        String str = "";

        JSONObject map=new JSONObject(true);//关键要设置为true，否则乱序


        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            value = entry.getValue();
            if (value.getClass()==String.class){
                value=value.toString();
            }

            returnMap.put(name, value);
            map.put(name, value);
        }
        return map;
    }



    public static <T> T getRequestEty(HttpServletRequest request, Class<T> clazz) {
        String json = JSON.toJSONString(getRequestString(request));
        json=json.replace("[","").replace("]","");
        return JSONObject.parseObject(json, clazz);
    }

    public static <T> T getRequestExampleEty(HttpServletRequest request, Class<T> clazz,Class<T> Exampleclsss) {
        String json = JSON.toJSONString(getRequestString(request));
        json=json.replace("[","").replace("]","");
        return JSONObject.parseObject(json, clazz);
    }
}
